package com.lanou.controller;

import com.lanou.bean.Users;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;


/**
 * @Author shw
 * @Description
 * @Param
 * @Date
 */
@Controller
@RequestMapping("/base")
public class BaseController {

    RestTemplate restTemplate = new RestTemplate();

    /**
     * @Author shw
     * @Description
     * @Param
     * @Date    会员登录,返回对象
     */

    @RequestMapping(value = "/loginForm.do", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map login(@RequestParam Map<String,Object> map) {
        MultiValueMap multiValueMap=new LinkedMultiValueMap();
        multiValueMap.setAll(map);
        //登录时返回结果(如果返回对象,证明可以登录)
        Users result = restTemplate.postForObject("http://10.90.87.240:9090/users/getUsersByUsers", multiValueMap,Users.class);

        Map<String,Object> map1=new HashMap<>();
        if (result!=null){
            map1.put("result","success");
            map1.put("loginUser",result.getUsername());
            return map1;
        }else {
            map1.put("result","message");
            return map1;
        }

    }

    /**
     * @Author shw
     * @Description
     * @Param
     * @Date   会员注册,跳转到注册信息填写页面
     */
    @RequestMapping("/register.do")
    @ResponseBody
    public ModelAndView toRegist(){
        ModelAndView modelAndView=new ModelAndView("base/register");
        return modelAndView;
    }

/**
 * @Author shw
 * @Description
 * @Param
 * @Date  会员注册验证  根据用户名查询对象,返回结果是对象,主要用来判断账号是否已经被注册
 */

    @RequestMapping(value = "/registerForm.do", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String, Object> registerAjax(@RequestParam Map<String,Object> map) {
        MultiValueMap multiValueMap=new LinkedMultiValueMap();
        multiValueMap.setAll(map);
        //调用接口,根据用户名验证是否可以进行注册
        Users result = restTemplate.postForObject("http://10.90.87.240:9090/users/getUsersByUsersName",multiValueMap,Users.class);
        if (result!=null){
            //已经存在的用户名,注册失败
            map.put("result","haven");
            return map;
        }else {
            int insert = restTemplate.postForObject("http://10.90.87.240:9090/users/insertNonEmptyUsers",multiValueMap,Integer.class);
            Map<String,Object> map1=new HashMap<>();

            if (insert>0){
                map1.put("result","success");
                System.out.println(map1);
                return map1;

            }else {
                map1.put("result","error");
                System.out.println(map1);
                return map1;
            }

        }
    }


/**
 * @Author shw
 * @Description
 * @Param
 * @Date  退出登录
 */
    @RequestMapping("/login.do")
    public String loginOut(){
        return "/base/login";
    }

}
